home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / prog_bas / decdef.zip / SCANFILE.BAS < prev   
BASIC Source File  |  1996-01-04  |  3KB  |  76 lines

  1. DefInt A-Z
  2.  
  3.  
  4. Declare Sub FOpen Lib "QPRO200.DLL" (ByVal FileName$, Handle%)
  5. Declare Sub FGet Lib "QPRO200.DLL" (ByVal Handle%, TEXT$)
  6. Declare Sub FSeek Lib "QPRO200.DLL" (ByVal Handle%, NewLocation&)
  7. Declare Sub FClose Lib "QPRO200.DLL" (ByVal Handle%)
  8.  
  9. Declare Function qInStr2% Lib "QPRO200.DLL" (ByVal start%, Source$, Search$)
  10. Declare Function DosError% Lib "QPRO200.DLL" ()
  11. Declare Function FLof& Lib "QPRO200.DLL" (ByVal Handle%)
  12.  
  13. Static Function ScanFile& (FileName$, TEXT$, start&)
  14.  
  15.    ScanFile& = -1                       'assume an error
  16.    L = 0                                'ditto
  17.  
  18.    FOpen FileName$, Handle              'open the file for QuickPak Binary
  19.    If DosError() Then Exit Function     'something bad must have happened
  20.    LENGTH& = FLof&(Handle)              'now get the file's length
  21.    If LENGTH& < 1 GoTo EXITFUNCTION     'can't search a zero-length file
  22.    If LENGTH& < Len(TEXT$) GoTo EXITFUNCTION    'text is longer than the file
  23.                                                 'so it can't possibly be there
  24.    BufSiz& = 4096                               '4096 evenly holds 8 sectors
  25.    If BufSiz& > LENGTH& Then BufSiz& = LENGTH&  'don't need more than Length&
  26.    TEMP$ = Space$(BufSiz&)                      'buffer for portions of file
  27.  
  28.    If start& < 1 Then start& = 1                'trap illegal start values
  29.    If start& > LENGTH& GoTo EXITFUNCTION        'can't start past the end!
  30.  
  31.    LASTSEEK& = start& - 1               'start searching where they asked
  32.    BASEADDR& = LASTSEEK&
  33.    BYTES = 0
  34.  
  35.    Do
  36.        BASEADDR& = BASEADDR& + BYTES    'track the start of each block
  37.  
  38.        If LENGTH& - LASTSEEK& >= BufSiz& Then
  39.           BYTES = BufSiz&               'at least BufSiz& bytes still to do
  40.        Else
  41.           BYTES = LENGTH& - LASTSEEK&   'get just what remains
  42.           TEMP$ = Space$(BYTES)         'FGet below uses LEN(Temp$) to know
  43.        End If                           '  how many bytes are to be read
  44.  
  45.        FSeek Handle, LASTSEEK&          'seek to that part of the file
  46.        FGet Handle, TEMP$               'read in a portion
  47.  
  48.        L = qInStr2%(1, TEMP$, TEXT$)    'search this portion of the file
  49.       'L = QInstr%(1, Temp$, Text$)     'or use this to honor capitalization
  50.  
  51.        If L Then Exit Do                'we found it!
  52.  
  53.        If BufSiz& + LASTSEEK& < LENGTH& Then 'still more to examine
  54.           BASEADDR& = BASEADDR& - Len(TEXT$) 'back up to avoid missing Text$
  55.                                              ' in case it straddles a boundary
  56.           LASTSEEK& = BASEADDR& + BYTES      'update the seek pointer
  57.        Else
  58.           Exit Do
  59.        End If
  60.  
  61.    Loop                                      'go examine some more
  62.  
  63. EXITFUNCTION:
  64.    FClose Handle                        'close the file
  65.  
  66.    If L Then
  67.       ScanFile& = BASEADDR& + L         'assign the function output
  68.    Else
  69.       ScanFile& = 0                     'no match was found
  70.    End If
  71.  
  72.    TEMP$ = ""                           'free up the memory
  73.  
  74. End Function
  75.  
  76.